package com.xy.sql.xml.node.impl;

import com.xy.sql.xml.core.XySQLExePara;
import com.xy.sql.xml.core.XySQLKit;
import com.xy.sql.xml.core.XySQLTaskContext;
import com.xy.sql.xml.itf.XySQLRegister;
import com.xy.sql.xml.node.XySQLSuperNode;

import java.util.ArrayList;
import java.util.List;

public class XySQLWhenNode extends XySQLSuperNode {

    @Override
    public String getTag() {
        return "when";
    }

    @Override
    public XySQLSuperNode clone() {
        return new XySQLWhenNode();
    }

    private static final String IF_ATT_COND = "test";

    private String newCond = null;

    /**
     * 是否通过
     *
     * @param task
     * @return
     */
    public boolean pass(XySQLTaskContext task) {
        if (newCond == null) {
            newCond = this.getPropertyMap().get(IF_ATT_COND);
        }
        return XySQLKit.runPredicate(newCond, task);
    }

    public List<XySQLExePara> boundSql(XySQLTaskContext task) throws Exception {
        if (pass(task)) {
            List<XySQLExePara> res = super.boundSql(task);
            return res;
        } else {
            XySQLRegister.getLogger().debug("if 公式(" + newCond + "),执行结果:false ");
        }
        return new ArrayList<>();
    }
}
